/**
 * @Author:  renyc
 * @Date: 2022-11-30 13:13:00
 */

'use strict';

import * as Cesium from 'cesium/Cesium';
import {LayerEventType} from '../event/EventType';
import LayerCollection from '../layer/LayerCollection';
import DataSourceCollection from '../data/source/DataSourceCollection';
import EventFactory from '../event/EventFactory';
import MouseEvent from '../event/MouseEvent';
import Transformer from '../utils/Transformer';

/**
 * 定义Cesium.Viewer的控件参数，默认关闭Viewer的控件。
 */
 const WIDGET_OPTS = {
    animation: false,                                   //关闭Widget
    baseLayerPicker: false,                             //关闭BaseLayerPicker Widget
    creditContainer: undefined,                         //关闭creditPicker Widget
    fullscreenButton:false,                             //关闭全屏控件
    geocoder:false,                                     //关闭地理编码控件
    homeButton:false,                                   //关闭Home控件
    infoBox:false,                                      //关闭infoBox控件
    navigationHelpButton: false,                        //关闭导航控件
    navigationInstructionsInitiallyVisible: false,
    sceneModePicker:false,                              //关闭场景模式(二、三维）控件
    selectionIndicator:false,                           //关闭选择器控件
    shouldAnimate:true,                                 //关闭动画显示控件
    timeline: false,                                    //关闭时间序列控件
    imageryProvider : false
 }

/**
 * @class View
 * 
 * @description 球的视图类
 */
class Viewer{

    constructor(container, options){
        this._name = "X-Viewer";        
 
        let opts = {
            ...WIDGET_OPTS,
            ...options
        };
        this._delegate = new Cesium.Viewer(container, {
            ...WIDGET_OPTS,
            ...options
        });
        this._delegate._cesiumWidget._creditContainer.style.display = "none";
        this._delegate.scene.debugShowFramesPerSecond = true;

        /**
         * 图层数组：用于记录Viewer上的图层
         * @protected
         */        
        this._layers = new LayerCollection();

        /**
         * 数据源数组：用户记录Viewer上的图层数据源         
         * @protected
         S* @Date 2023年3月2日20:55:01
         */
        this._sources = new DataSourceCollection();


        this._mouseEvent = EventFactory.createMouseEvent(this);
    };

    /**
     * 获取Earth的名称
     * @method
     */
    get name(){
        return this._name;
    };

    /**
     * 获取{Cesium.Viewer}
     * @method
     */
    get delegate(){
        return this._delegate;
    }

    /**
     * 获取Cesium.Viewer.container
     */
    get container(){
        return this._delegate.container;
    }

    /**
     * 获取Cesium.Viewer.Scene
     */
    get scene(){
        return this._delegate.scene;
    }

    /**
     * 获取Cesium.Viewer.Camera
     */
    get camera(){
        return this._delegate.camera;
    }

    /**
     * 获取Cesium.Viewer.entities
     */
    get entities(){
        return this._delegate.entities;
    }

    get layers(){
        return this._layers.getLayers();
    } 

    /**
     * 获取OneEarth.DataSource数组
     * @Date 2023年3月2日20:56:48
     */
    get sources(){
        return this._sources;
    }

    /**
     * 添加OneEarth.DataSource
     * @param {} source 
     * @Date 2023年3月2日21:02:39
     */
    addSource(source){
        return this._sources.add(source);
    }

    /**
     * 获取OneEarth.DataSource
     * @param {} name 
     * @returns OneEarth.DataSource
     */
    getSource(name){
        return this._sources.get(name);
    }

    /**
     * 获取Cesium.Viewer.Scene.primitives
     */
    get primitives(){
        return this._delegate.scene.primitives;
    }

    /**
     * 获取鼠标事件
     */
    get mouseEvent(){
        return this._mouseEvent;
    }

    /**
     * 设置Cesium.Viewer的Terrain数据
     * @param {TerrainProvider} terrain 
     * 
     * @example
     * let terrain = TerrainFactory.createTerrain(TerrainType.CESIUM);
        earth.setTerrain(terrain);
     */
    setTerrain(terrain){
        if( !this._delegate || !terrain){
            return;
        }

        this._delegate.terrainProvider = terrain;
    };

    /**
     * 添加BaseLayer
     * @param {Cesium.ImageryProvider} provider 
     */
    addBaseLayer(provider){
        if(!provider || !this._delegate){
            return;
        }

        // if(!this._delegate.imageryProvider){
        //     this._delegate.imageryProvider = provider;
        // }
        //this._delegate.imageryLayers.add(provider);
        this._delegate.scene.imageryLayers.addImageryProvider(provider);
    };

    /**
     * 添加图层
     * @param {Layer} layer 
     */
    addLayer(layer){
        if(!layer){
            throw new Error('Earth: layer无效');
        }
        if(!this._layers.find(layer.name)){
            layer.attach(this);
            this._layers.add(layer);
        }
    }

    /**
     * 删除图层
     * @param {Layer} layer 
     */
    removeLayer(layer){
        if(this._layers.find(layer.name)){
            //layer.fire(LayerEventType.REMOVE, this);
            layer.detach();
            this._layers.remove(layer);
        }
    }

    /**
     * 根据图层名获取图层
     * @param {String} name 
     */
    getLayer(name){
        return this._layers.find(name);
    }

    /**
     * 返回Cesium.Viewer的dataSources属性
     * returns {Cesium.DataSourceCollection}
     * @deprecated(2023年3月2日21:00:59)     
     */
    get dataSources() {
        return this._delegate.dataSources;
    }

    /**
     * 视图飞行
     * @param {*} target 
     * @param {*} duration 
     * @returns this
     */
    flyTo(target, duration){
        this._delegate.flyTo(target.delegate, {
            duration
        })
        return this;
    }

    /**
     * 视图缩放
     * @returns Viewer引用
     */
    zoomTo(target){
        //注释：2023-02-09, renyc。Layer增加Source属性后，更新zoomTo的实现方式
        // this._delegate.zoomTo(target?.delegate || target);

        if(!target){
            return this;
        }
        // if(target.source){
        //     this._delegate.zoomTo(target.source.delegate);
        // }
        else if(target.delegate){
            this._delegate.zoomTo(target.delegate);
        }
        
        return this;
    }

    /**
     * Viewer飞行到指定Position
     * @param {*} position 
     * @param {*} orientation 
     * @param {*} completeCallback 
     * @param {*} duration 
     * 
     * position: {
     *      lng : 116.0,
     *      lat : 39.0,
     *      alt : 1000.0
     * }
     * orientation : {
     *      heading
     * }
     */
    flyToPosition(position, orientation, completeCallback, duration) {
        if(!position){
            return;
        }
        if(!orientation){
            this.camera.flyTo({
                destination: Transformer.transformWGS84ToCartesian(position),
                complete: completeCallback,
                duration: duration
              });
        }
        else{
            this.camera.flyTo({
                destination: Transform.transformWGS84ToCartesian(position),
                orientation: {
                    heading : Cesium.Math.toRadians(orientation.heading),
                    pitch : Cesium.Math.toRadians(orientation.pitch),
                    roll : Cesium.Math.toRadians(orientation.roll)
                },
                complete: completeCallback,
                duration: duration
            });
        }
        return this;
    }

    zoomToPosition(position, orientation, completeCallback) {
        this.flyToPosition(position, orientation, completeCallback, 0)
        return this;
    }
}

export default Viewer